//
//  CacheHtmlFile.m
//  Mamaji
//
//  Created by 玛马吉 on 2017/3/31.
//  Copyright © 2017年 玛马吉. All rights reserved.
//

#import "CacheHtmlFile.h"

@implementation CacheHtmlFile
+ (NSString *)readToWebCacheFileWithUrlString:(NSString *)urlString{
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) objectAtIndex:0];
    NSString * pathString = [NSString stringWithFormat:@"wwwww"];
    NSString * path = [cachesPath stringByAppendingString:[NSString stringWithFormat:@"/Caches/%lu.html",(unsigned long)[pathString hash]]];
    NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    return htmlString;
}

+ (void)writeToWebCacheFileWithString:(NSString *)string{
    dispatch_async(dispatch_queue_create(0, 0), ^{
        NSString * htmlResponseStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:string] encoding:NSUTF8StringEncoding error:Nil];
        //创建文件管理器
        NSFileManager *fileManager = [[NSFileManager alloc] init];
        //获取document路径
        NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        [fileManager createDirectoryAtPath:[cachesPath stringByAppendingString:@"/Caches"] withIntermediateDirectories:YES attributes:nil error:nil];
        NSString * pathString = [NSString stringWithFormat:@"kkkk"];
        //写入路径
        NSString * path = [cachesPath stringByAppendingString:[NSString stringWithFormat:@"/Caches/%lu.html", (unsigned long)[pathString hash]]];
        [htmlResponseStr writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
    });
}

+ (float)getHtmlFileSize{
    NSString *paths = [CacheHtmlFile getCachesFilePath];
    return [CacheHtmlFile fileSizeForDir:paths];
}

//计算文件夹下文件的总大小
+ (float)fileSizeForDir:(NSString *)path{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    float size =0;
    NSArray* array = [fileManager contentsOfDirectoryAtPath:path error:nil];
    for(int i = 0; i<[array count]; i++){
        NSString *fullPath = [path stringByAppendingPathComponent:[array objectAtIndex:i]];
        BOOL isDir;
        if ( !([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && isDir) ){
            NSDictionary *fileAttributeDic=[fileManager attributesOfItemAtPath:fullPath error:nil];
            size += fileAttributeDic.fileSize;
        }else{
            [self fileSizeForDir:fullPath];
        }
    }
    return size;
}

+ (void)removeHtmlFileCaches {
    NSFileManager * fileManager = [NSFileManager defaultManager];
    NSString *paths = [CacheHtmlFile getCachesFilePath];
    NSArray* array = [fileManager contentsOfDirectoryAtPath:paths error:nil];
    for (int i = 0; i < array.count; i++) {
        NSString * path = [paths stringByAppendingString:[NSString stringWithFormat:@"%@", array[i]]];
        [fileManager removeItemAtPath:path error:nil];
    }
}

+ (NSString *)getCachesFilePath{
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString * paths = [cachesPath stringByAppendingString:@"/Caches/"];
    return paths;
}
@end
